package com.wmh.baseservice.system.service.impl;

import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.merchant.pojo.MerchantIncomeExpend;
import com.wmh.baseservice.merchant.pojo.MerchantInfo;
import com.wmh.baseservice.merchant.service.MerchantIncomeExpendService;
import com.wmh.baseservice.merchant.service.MerchantInfoService;
import com.wmh.baseservice.system.pojo.SysRedEnvelope;
import com.wmh.baseservice.system.mapper.SysRedEnvelopeMapper;
import com.wmh.baseservice.system.service.SysRedEnvelopeService;
import com.wmh.baseservice.common.mybatisplus.methods.CommonServiceImpl;
import com.wmh.baseservice.user.pojo.UsIncomeExpend;
import com.wmh.baseservice.user.pojo.UsInfo;
import com.wmh.baseservice.user.service.UsIncomeExpendService;
import com.wmh.baseservice.user.service.UsInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author wmh generator
 * @since 2022-08-02
 */
@Service
public class SysRedEnvelopeServiceImpl extends CommonServiceImpl<SysRedEnvelopeMapper, SysRedEnvelope> implements SysRedEnvelopeService {

    @Resource
    private UsInfoService usInfoService;
    @Resource
    private MerchantInfoService merchantInfoService;
    @Resource
    private MerchantIncomeExpendService merchantIncomeExpendService;
    @Resource
    private UsIncomeExpendService usIncomeExpendService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void receive(SysRedEnvelope sysRedEnvelope) {
        sysRedEnvelope.setStatus(1);
        //红包余额是提前扣减的，所以直接将余额转给对方
        if (sysRedEnvelope.getSendType() == 0) {
            UsInfo usInfo = usInfoService.getByIdForUpdate(sysRedEnvelope.getReceiveId());
            usInfo.setBalance(usInfo.getBalance().add(sysRedEnvelope.getMoney()));
            if (!usInfoService.updateById(usInfo)) {
                throw new ServiceException();
            }
            //添加用户流水
            UsIncomeExpend usIncomeexpend = new UsIncomeExpend().setRemark("红包").setMoney(sysRedEnvelope.getMoney()).setStatus(1).setType(1).setUsId(usInfo.getId());
            if (!usIncomeExpendService.save(usIncomeexpend)) {
                throw new ServiceException();
            }
        } else {
            MerchantInfo merchantInfo = merchantInfoService.getByIdForUpdate(sysRedEnvelope.getReceiveId());
            merchantInfo.setBalance(merchantInfo.getBalance().add(sysRedEnvelope.getMoney()));
            if (!merchantInfoService.updateById(merchantInfo)) {
                throw new ServiceException();
            }
            //添加商家流水
            MerchantIncomeExpend merchantIncomeExpend = new MerchantIncomeExpend().setMerchantId(merchantInfo.getId()).setRemark("红包").setMoney(sysRedEnvelope.getMoney()).setStatus(1);
            if (!merchantIncomeExpendService.save(merchantIncomeExpend)) {
                throw new ServiceException();
            }
        }
        //修改红包状态
        if (!updateById(sysRedEnvelope)) {
            throw new ServiceException();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendRedEnvelopeData(SysRedEnvelope sysRedEnvelope) {
        if (sysRedEnvelope.getSendType() == 0) {
            //添加商家支出流水
            MerchantIncomeExpend merchantIncomeExpend = new MerchantIncomeExpend().setMoney(sysRedEnvelope.getMoney()).setRemark("红包支出").setMerchantId(sysRedEnvelope.getSendId()).setStatus(-1);
            //余额扣减
            MerchantInfo merchantInfo = merchantInfoService.getByIdForUpdate(merchantIncomeExpend.getMerchantId());
            merchantInfo.setBalance(merchantInfo.getBalance().subtract(sysRedEnvelope.getMoney()));
            if (!merchantInfoService.updateById(merchantInfo)) {
                throw new ServiceException();
            }
            if (!merchantIncomeExpendService.save(merchantIncomeExpend)) {
                throw new ServiceException();
            }
        } else {
            //添加用户支出流水
            UsIncomeExpend usIncomeExpend = new UsIncomeExpend().setUsId(sysRedEnvelope.getSendId()).setMoney(sysRedEnvelope.getMoney()).setRemark("红包支出").setStatus(-1);
            //余额扣减
            UsInfo usInfo = usInfoService.getByIdForUpdate(usIncomeExpend.getUsId());
            usInfo.setBalance(usInfo.getBalance().subtract(sysRedEnvelope.getMoney()));
            if (!usInfoService.updateById(usInfo)) {
                throw new ServiceException();
            }
            if (!usIncomeExpendService.save(usIncomeExpend)) {
                throw new ServiceException();
            }
        }
        if (!save(sysRedEnvelope)) {
            throw new ServiceException();
        }
    }
}
